# Offline Docker image build script for Dinghong Cloud Screen project
# This script tries to build images without downloading from Docker Hub

$ErrorActionPreference = "Stop"

Write-Host "Starting offline build for Dinghong Cloud Screen project..." -ForegroundColor Green

# Get project root directory
$PROJECT_ROOT = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path))
Write-Host "Project root directory: $PROJECT_ROOT" -ForegroundColor Cyan

# Check if we have local base images
Write-Host "`nChecking for local base images..." -ForegroundColor Yellow

$localImages = @(
    "alpine:latest",
    "golang:alpine",
    "node:20-slim",
    "nginx:alpine",
    "mysql:8.0.21",
    "redis:6.0.6"
)

$availableImages = @()
foreach ($image in $localImages) {
    $imageExists = docker images $image --format "table {{.Repository}}:{{.Tag}}" 2>$null | Select-String $image
    if ($imageExists) {
        Write-Host "✓ Found local image: $image" -ForegroundColor Green
        $availableImages += $image
    }
    else {
        Write-Host "✗ Missing local image: $image" -ForegroundColor Red
    }
}

# Check server directory
$serverPath = Join-Path $PROJECT_ROOT "server"
if (Test-Path $serverPath) {
    Write-Host "✓ Server directory found at: $serverPath" -ForegroundColor Green
}
else {
    Write-Host "✗ Server directory not found" -ForegroundColor Red
    exit 1
}

# Check web directory
$webPath = Join-Path $PROJECT_ROOT "web"
if (Test-Path $webPath) {
    Write-Host "✓ Web directory found at: $webPath" -ForegroundColor Green
}
else {
    Write-Host "✗ Web directory not found" -ForegroundColor Red
    exit 1
}

# Try to build with network options
Write-Host "`nAttempting to build images with network options..." -ForegroundColor Yellow

# Build backend image with network options
Write-Host "Building backend image..." -ForegroundColor Yellow
Set-Location $serverPath

# Try different build strategies
$buildStrategies = @(
    "docker build --network=host -t dinghong-cloud-screen-server:latest .",
    "docker build --no-cache --network=host -t dinghong-cloud-screen-server:latest .",
    "docker build --build-arg HTTP_PROXY=http://127.0.0.1:7890 --build-arg HTTPS_PROXY=http://127.0.0.1:7890 -t dinghong-cloud-screen-server:latest ."
)

$backendBuilt = $false
foreach ($strategy in $buildStrategies) {
    Write-Host "Trying: $strategy" -ForegroundColor Cyan
    Invoke-Expression $strategy
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✓ Backend image built successfully with: $strategy" -ForegroundColor Green
        $backendBuilt = $true
        break
    }
    else {
        Write-Host "✗ Failed with: $strategy" -ForegroundColor Red
    }
}

if (-not $backendBuilt) {
    Write-Host "✗ All backend build strategies failed" -ForegroundColor Red
    Write-Host "Please check your network connection or try the network fix script" -ForegroundColor Yellow
    exit 1
}

# Build frontend image with network options
Write-Host "Building frontend image..." -ForegroundColor Yellow
Set-Location $webPath

$frontendBuilt = $false
foreach ($strategy in $buildStrategies) {
    Write-Host "Trying: $strategy" -ForegroundColor Cyan
    Invoke-Expression $strategy
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✓ Frontend image built successfully with: $strategy" -ForegroundColor Green
        $frontendBuilt = $true
        break
    }
    else {
        Write-Host "✗ Failed with: $strategy" -ForegroundColor Red
    }
}

if (-not $frontendBuilt) {
    Write-Host "✗ All frontend build strategies failed" -ForegroundColor Red
    Write-Host "Please check your network connection or try the network fix script" -ForegroundColor Yellow
    exit 1
}

# Load images to Minikube
Write-Host "Loading images to Minikube..." -ForegroundColor Yellow
minikube image load dinghong-cloud-screen-server:latest
minikube image load dinghong-cloud-screen-web:latest

Write-Host "`nOffline build completed!" -ForegroundColor Green
Write-Host "Backend image: dinghong-cloud-screen-server:latest" -ForegroundColor Cyan
Write-Host "Frontend image: dinghong-cloud-screen-web:latest" -ForegroundColor Cyan

Write-Host "`nIf you still have network issues, try:" -ForegroundColor Yellow
Write-Host "1. Run: .\fix-docker-network.ps1" -ForegroundColor White
Write-Host "2. Restart Docker Desktop" -ForegroundColor White
Write-Host "3. Use a VPN or different network" -ForegroundColor White 